/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MeteoCal.controller;

import MeteoCal.bean.exceptions.IllegalOrphanException;
import MeteoCal.bean.exceptions.NonexistentEntityException;
import MeteoCal.bean.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MeteoCal.entity.User;
import MeteoCal.entity.Invitation;
import java.util.ArrayList;
import java.util.Collection;
import MeteoCal.entity.Schedule;
import MeteoCal.entity.Notification;
import MeteoCal.entity.Warningevent;
import MeteoCal.entity.Commentwritten;
import MeteoCal.entity.Comment;
import MeteoCal.entity.Event;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author Daniele
 */
public class EventJpaController implements Serializable {

    public EventJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Event event) throws RollbackFailureException, Exception {
        if (event.getInvitationCollection() == null) {
            event.setInvitationCollection(new ArrayList<Invitation>());
        }
        if (event.getScheduleCollection() == null) {
            event.setScheduleCollection(new ArrayList<Schedule>());
        }
        if (event.getNotificationCollection() == null) {
            event.setNotificationCollection(new ArrayList<Notification>());
        }
        if (event.getWarningeventCollection() == null) {
            event.setWarningeventCollection(new ArrayList<Warningevent>());
        }
        if (event.getCommentwrittenCollection() == null) {
            event.setCommentwrittenCollection(new ArrayList<Commentwritten>());
        }
        if (event.getCommentCollection() == null) {
            event.setCommentCollection(new ArrayList<Comment>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            User idCreator = event.getIdCreator();
            if (idCreator != null) {
                idCreator = em.getReference(idCreator.getClass(), idCreator.getEmail());
                event.setIdCreator(idCreator);
            }
            Collection<Invitation> attachedInvitationCollection = new ArrayList<Invitation>();
            for (Invitation invitationCollectionInvitationToAttach : event.getInvitationCollection()) {
                invitationCollectionInvitationToAttach = em.getReference(invitationCollectionInvitationToAttach.getClass(), invitationCollectionInvitationToAttach.getInvitationPK());
                attachedInvitationCollection.add(invitationCollectionInvitationToAttach);
            }
            event.setInvitationCollection(attachedInvitationCollection);
            Collection<Schedule> attachedScheduleCollection = new ArrayList<Schedule>();
            for (Schedule scheduleCollectionScheduleToAttach : event.getScheduleCollection()) {
                scheduleCollectionScheduleToAttach = em.getReference(scheduleCollectionScheduleToAttach.getClass(), scheduleCollectionScheduleToAttach.getIdSchedule());
                attachedScheduleCollection.add(scheduleCollectionScheduleToAttach);
            }
            event.setScheduleCollection(attachedScheduleCollection);
            Collection<Notification> attachedNotificationCollection = new ArrayList<Notification>();
            for (Notification notificationCollectionNotificationToAttach : event.getNotificationCollection()) {
                notificationCollectionNotificationToAttach = em.getReference(notificationCollectionNotificationToAttach.getClass(), notificationCollectionNotificationToAttach.getIdNotification());
                attachedNotificationCollection.add(notificationCollectionNotificationToAttach);
            }
            event.setNotificationCollection(attachedNotificationCollection);
            Collection<Warningevent> attachedWarningeventCollection = new ArrayList<Warningevent>();
            for (Warningevent warningeventCollectionWarningeventToAttach : event.getWarningeventCollection()) {
                warningeventCollectionWarningeventToAttach = em.getReference(warningeventCollectionWarningeventToAttach.getClass(), warningeventCollectionWarningeventToAttach.getWarningeventPK());
                attachedWarningeventCollection.add(warningeventCollectionWarningeventToAttach);
            }
            event.setWarningeventCollection(attachedWarningeventCollection);
            Collection<Commentwritten> attachedCommentwrittenCollection = new ArrayList<Commentwritten>();
            for (Commentwritten commentwrittenCollectionCommentwrittenToAttach : event.getCommentwrittenCollection()) {
                commentwrittenCollectionCommentwrittenToAttach = em.getReference(commentwrittenCollectionCommentwrittenToAttach.getClass(), commentwrittenCollectionCommentwrittenToAttach.getCommentwrittenPK());
                attachedCommentwrittenCollection.add(commentwrittenCollectionCommentwrittenToAttach);
            }
            event.setCommentwrittenCollection(attachedCommentwrittenCollection);
            Collection<Comment> attachedCommentCollection = new ArrayList<Comment>();
            for (Comment commentCollectionCommentToAttach : event.getCommentCollection()) {
                commentCollectionCommentToAttach = em.getReference(commentCollectionCommentToAttach.getClass(), commentCollectionCommentToAttach.getIdComment());
                attachedCommentCollection.add(commentCollectionCommentToAttach);
            }
            event.setCommentCollection(attachedCommentCollection);
            em.persist(event);
            if (idCreator != null) {
                idCreator.getEventCollection().add(event);
                idCreator = em.merge(idCreator);
            }
            for (Invitation invitationCollectionInvitation : event.getInvitationCollection()) {
                Event oldEventOfInvitationCollectionInvitation = invitationCollectionInvitation.getEvent();
                invitationCollectionInvitation.setEvent(event);
                invitationCollectionInvitation = em.merge(invitationCollectionInvitation);
                if (oldEventOfInvitationCollectionInvitation != null) {
                    oldEventOfInvitationCollectionInvitation.getInvitationCollection().remove(invitationCollectionInvitation);
                    oldEventOfInvitationCollectionInvitation = em.merge(oldEventOfInvitationCollectionInvitation);
                }
            }
            for (Schedule scheduleCollectionSchedule : event.getScheduleCollection()) {
                scheduleCollectionSchedule.getEventCollection().add(event);
                scheduleCollectionSchedule = em.merge(scheduleCollectionSchedule);
            }
            for (Notification notificationCollectionNotification : event.getNotificationCollection()) {
                Event oldIdEventOfNotificationCollectionNotification = notificationCollectionNotification.getIdEvent();
                notificationCollectionNotification.setIdEvent(event);
                notificationCollectionNotification = em.merge(notificationCollectionNotification);
                if (oldIdEventOfNotificationCollectionNotification != null) {
                    oldIdEventOfNotificationCollectionNotification.getNotificationCollection().remove(notificationCollectionNotification);
                    oldIdEventOfNotificationCollectionNotification = em.merge(oldIdEventOfNotificationCollectionNotification);
                }
            }
            for (Warningevent warningeventCollectionWarningevent : event.getWarningeventCollection()) {
                Event oldEventOfWarningeventCollectionWarningevent = warningeventCollectionWarningevent.getEvent();
                warningeventCollectionWarningevent.setEvent(event);
                warningeventCollectionWarningevent = em.merge(warningeventCollectionWarningevent);
                if (oldEventOfWarningeventCollectionWarningevent != null) {
                    oldEventOfWarningeventCollectionWarningevent.getWarningeventCollection().remove(warningeventCollectionWarningevent);
                    oldEventOfWarningeventCollectionWarningevent = em.merge(oldEventOfWarningeventCollectionWarningevent);
                }
            }
            for (Commentwritten commentwrittenCollectionCommentwritten : event.getCommentwrittenCollection()) {
                Event oldEventOfCommentwrittenCollectionCommentwritten = commentwrittenCollectionCommentwritten.getEvent();
                commentwrittenCollectionCommentwritten.setEvent(event);
                commentwrittenCollectionCommentwritten = em.merge(commentwrittenCollectionCommentwritten);
                if (oldEventOfCommentwrittenCollectionCommentwritten != null) {
                    oldEventOfCommentwrittenCollectionCommentwritten.getCommentwrittenCollection().remove(commentwrittenCollectionCommentwritten);
                    oldEventOfCommentwrittenCollectionCommentwritten = em.merge(oldEventOfCommentwrittenCollectionCommentwritten);
                }
            }
            for (Comment commentCollectionComment : event.getCommentCollection()) {
                Event oldIdEventOfCommentCollectionComment = commentCollectionComment.getIdEvent();
                commentCollectionComment.setIdEvent(event);
                commentCollectionComment = em.merge(commentCollectionComment);
                if (oldIdEventOfCommentCollectionComment != null) {
                    oldIdEventOfCommentCollectionComment.getCommentCollection().remove(commentCollectionComment);
                    oldIdEventOfCommentCollectionComment = em.merge(oldIdEventOfCommentCollectionComment);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Event event) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Event persistentEvent = em.find(Event.class, event.getIdEvent());
            User idCreatorOld = persistentEvent.getIdCreator();
            User idCreatorNew = event.getIdCreator();
            Collection<Invitation> invitationCollectionOld = persistentEvent.getInvitationCollection();
            Collection<Invitation> invitationCollectionNew = event.getInvitationCollection();
            Collection<Schedule> scheduleCollectionOld = persistentEvent.getScheduleCollection();
            Collection<Schedule> scheduleCollectionNew = event.getScheduleCollection();
            Collection<Notification> notificationCollectionOld = persistentEvent.getNotificationCollection();
            Collection<Notification> notificationCollectionNew = event.getNotificationCollection();
            Collection<Warningevent> warningeventCollectionOld = persistentEvent.getWarningeventCollection();
            Collection<Warningevent> warningeventCollectionNew = event.getWarningeventCollection();
            Collection<Commentwritten> commentwrittenCollectionOld = persistentEvent.getCommentwrittenCollection();
            Collection<Commentwritten> commentwrittenCollectionNew = event.getCommentwrittenCollection();
            Collection<Comment> commentCollectionOld = persistentEvent.getCommentCollection();
            Collection<Comment> commentCollectionNew = event.getCommentCollection();
            List<String> illegalOrphanMessages = null;
            for (Invitation invitationCollectionOldInvitation : invitationCollectionOld) {
                if (!invitationCollectionNew.contains(invitationCollectionOldInvitation)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Invitation " + invitationCollectionOldInvitation + " since its event field is not nullable.");
                }
            }
            for (Notification notificationCollectionOldNotification : notificationCollectionOld) {
                if (!notificationCollectionNew.contains(notificationCollectionOldNotification)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Notification " + notificationCollectionOldNotification + " since its idEvent field is not nullable.");
                }
            }
            for (Warningevent warningeventCollectionOldWarningevent : warningeventCollectionOld) {
                if (!warningeventCollectionNew.contains(warningeventCollectionOldWarningevent)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Warningevent " + warningeventCollectionOldWarningevent + " since its event field is not nullable.");
                }
            }
            for (Commentwritten commentwrittenCollectionOldCommentwritten : commentwrittenCollectionOld) {
                if (!commentwrittenCollectionNew.contains(commentwrittenCollectionOldCommentwritten)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Commentwritten " + commentwrittenCollectionOldCommentwritten + " since its event field is not nullable.");
                }
            }
            for (Comment commentCollectionOldComment : commentCollectionOld) {
                if (!commentCollectionNew.contains(commentCollectionOldComment)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Comment " + commentCollectionOldComment + " since its idEvent field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (idCreatorNew != null) {
                idCreatorNew = em.getReference(idCreatorNew.getClass(), idCreatorNew.getEmail());
                event.setIdCreator(idCreatorNew);
            }
            Collection<Invitation> attachedInvitationCollectionNew = new ArrayList<Invitation>();
            for (Invitation invitationCollectionNewInvitationToAttach : invitationCollectionNew) {
                invitationCollectionNewInvitationToAttach = em.getReference(invitationCollectionNewInvitationToAttach.getClass(), invitationCollectionNewInvitationToAttach.getInvitationPK());
                attachedInvitationCollectionNew.add(invitationCollectionNewInvitationToAttach);
            }
            invitationCollectionNew = attachedInvitationCollectionNew;
            event.setInvitationCollection(invitationCollectionNew);
            Collection<Schedule> attachedScheduleCollectionNew = new ArrayList<Schedule>();
            for (Schedule scheduleCollectionNewScheduleToAttach : scheduleCollectionNew) {
                scheduleCollectionNewScheduleToAttach = em.getReference(scheduleCollectionNewScheduleToAttach.getClass(), scheduleCollectionNewScheduleToAttach.getIdSchedule());
                attachedScheduleCollectionNew.add(scheduleCollectionNewScheduleToAttach);
            }
            scheduleCollectionNew = attachedScheduleCollectionNew;
            event.setScheduleCollection(scheduleCollectionNew);
            Collection<Notification> attachedNotificationCollectionNew = new ArrayList<Notification>();
            for (Notification notificationCollectionNewNotificationToAttach : notificationCollectionNew) {
                notificationCollectionNewNotificationToAttach = em.getReference(notificationCollectionNewNotificationToAttach.getClass(), notificationCollectionNewNotificationToAttach.getIdNotification());
                attachedNotificationCollectionNew.add(notificationCollectionNewNotificationToAttach);
            }
            notificationCollectionNew = attachedNotificationCollectionNew;
            event.setNotificationCollection(notificationCollectionNew);
            Collection<Warningevent> attachedWarningeventCollectionNew = new ArrayList<Warningevent>();
            for (Warningevent warningeventCollectionNewWarningeventToAttach : warningeventCollectionNew) {
                warningeventCollectionNewWarningeventToAttach = em.getReference(warningeventCollectionNewWarningeventToAttach.getClass(), warningeventCollectionNewWarningeventToAttach.getWarningeventPK());
                attachedWarningeventCollectionNew.add(warningeventCollectionNewWarningeventToAttach);
            }
            warningeventCollectionNew = attachedWarningeventCollectionNew;
            event.setWarningeventCollection(warningeventCollectionNew);
            Collection<Commentwritten> attachedCommentwrittenCollectionNew = new ArrayList<Commentwritten>();
            for (Commentwritten commentwrittenCollectionNewCommentwrittenToAttach : commentwrittenCollectionNew) {
                commentwrittenCollectionNewCommentwrittenToAttach = em.getReference(commentwrittenCollectionNewCommentwrittenToAttach.getClass(), commentwrittenCollectionNewCommentwrittenToAttach.getCommentwrittenPK());
                attachedCommentwrittenCollectionNew.add(commentwrittenCollectionNewCommentwrittenToAttach);
            }
            commentwrittenCollectionNew = attachedCommentwrittenCollectionNew;
            event.setCommentwrittenCollection(commentwrittenCollectionNew);
            Collection<Comment> attachedCommentCollectionNew = new ArrayList<Comment>();
            for (Comment commentCollectionNewCommentToAttach : commentCollectionNew) {
                commentCollectionNewCommentToAttach = em.getReference(commentCollectionNewCommentToAttach.getClass(), commentCollectionNewCommentToAttach.getIdComment());
                attachedCommentCollectionNew.add(commentCollectionNewCommentToAttach);
            }
            commentCollectionNew = attachedCommentCollectionNew;
            event.setCommentCollection(commentCollectionNew);
            event = em.merge(event);
            if (idCreatorOld != null && !idCreatorOld.equals(idCreatorNew)) {
                idCreatorOld.getEventCollection().remove(event);
                idCreatorOld = em.merge(idCreatorOld);
            }
            if (idCreatorNew != null && !idCreatorNew.equals(idCreatorOld)) {
                idCreatorNew.getEventCollection().add(event);
                idCreatorNew = em.merge(idCreatorNew);
            }
            for (Invitation invitationCollectionNewInvitation : invitationCollectionNew) {
                if (!invitationCollectionOld.contains(invitationCollectionNewInvitation)) {
                    Event oldEventOfInvitationCollectionNewInvitation = invitationCollectionNewInvitation.getEvent();
                    invitationCollectionNewInvitation.setEvent(event);
                    invitationCollectionNewInvitation = em.merge(invitationCollectionNewInvitation);
                    if (oldEventOfInvitationCollectionNewInvitation != null && !oldEventOfInvitationCollectionNewInvitation.equals(event)) {
                        oldEventOfInvitationCollectionNewInvitation.getInvitationCollection().remove(invitationCollectionNewInvitation);
                        oldEventOfInvitationCollectionNewInvitation = em.merge(oldEventOfInvitationCollectionNewInvitation);
                    }
                }
            }
            for (Schedule scheduleCollectionOldSchedule : scheduleCollectionOld) {
                if (!scheduleCollectionNew.contains(scheduleCollectionOldSchedule)) {
                    scheduleCollectionOldSchedule.getEventCollection().remove(event);
                    scheduleCollectionOldSchedule = em.merge(scheduleCollectionOldSchedule);
                }
            }
            for (Schedule scheduleCollectionNewSchedule : scheduleCollectionNew) {
                if (!scheduleCollectionOld.contains(scheduleCollectionNewSchedule)) {
                    scheduleCollectionNewSchedule.getEventCollection().add(event);
                    scheduleCollectionNewSchedule = em.merge(scheduleCollectionNewSchedule);
                }
            }
            for (Notification notificationCollectionNewNotification : notificationCollectionNew) {
                if (!notificationCollectionOld.contains(notificationCollectionNewNotification)) {
                    Event oldIdEventOfNotificationCollectionNewNotification = notificationCollectionNewNotification.getIdEvent();
                    notificationCollectionNewNotification.setIdEvent(event);
                    notificationCollectionNewNotification = em.merge(notificationCollectionNewNotification);
                    if (oldIdEventOfNotificationCollectionNewNotification != null && !oldIdEventOfNotificationCollectionNewNotification.equals(event)) {
                        oldIdEventOfNotificationCollectionNewNotification.getNotificationCollection().remove(notificationCollectionNewNotification);
                        oldIdEventOfNotificationCollectionNewNotification = em.merge(oldIdEventOfNotificationCollectionNewNotification);
                    }
                }
            }
            for (Warningevent warningeventCollectionNewWarningevent : warningeventCollectionNew) {
                if (!warningeventCollectionOld.contains(warningeventCollectionNewWarningevent)) {
                    Event oldEventOfWarningeventCollectionNewWarningevent = warningeventCollectionNewWarningevent.getEvent();
                    warningeventCollectionNewWarningevent.setEvent(event);
                    warningeventCollectionNewWarningevent = em.merge(warningeventCollectionNewWarningevent);
                    if (oldEventOfWarningeventCollectionNewWarningevent != null && !oldEventOfWarningeventCollectionNewWarningevent.equals(event)) {
                        oldEventOfWarningeventCollectionNewWarningevent.getWarningeventCollection().remove(warningeventCollectionNewWarningevent);
                        oldEventOfWarningeventCollectionNewWarningevent = em.merge(oldEventOfWarningeventCollectionNewWarningevent);
                    }
                }
            }
            for (Commentwritten commentwrittenCollectionNewCommentwritten : commentwrittenCollectionNew) {
                if (!commentwrittenCollectionOld.contains(commentwrittenCollectionNewCommentwritten)) {
                    Event oldEventOfCommentwrittenCollectionNewCommentwritten = commentwrittenCollectionNewCommentwritten.getEvent();
                    commentwrittenCollectionNewCommentwritten.setEvent(event);
                    commentwrittenCollectionNewCommentwritten = em.merge(commentwrittenCollectionNewCommentwritten);
                    if (oldEventOfCommentwrittenCollectionNewCommentwritten != null && !oldEventOfCommentwrittenCollectionNewCommentwritten.equals(event)) {
                        oldEventOfCommentwrittenCollectionNewCommentwritten.getCommentwrittenCollection().remove(commentwrittenCollectionNewCommentwritten);
                        oldEventOfCommentwrittenCollectionNewCommentwritten = em.merge(oldEventOfCommentwrittenCollectionNewCommentwritten);
                    }
                }
            }
            for (Comment commentCollectionNewComment : commentCollectionNew) {
                if (!commentCollectionOld.contains(commentCollectionNewComment)) {
                    Event oldIdEventOfCommentCollectionNewComment = commentCollectionNewComment.getIdEvent();
                    commentCollectionNewComment.setIdEvent(event);
                    commentCollectionNewComment = em.merge(commentCollectionNewComment);
                    if (oldIdEventOfCommentCollectionNewComment != null && !oldIdEventOfCommentCollectionNewComment.equals(event)) {
                        oldIdEventOfCommentCollectionNewComment.getCommentCollection().remove(commentCollectionNewComment);
                        oldIdEventOfCommentCollectionNewComment = em.merge(oldIdEventOfCommentCollectionNewComment);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = event.getIdEvent();
                if (findEvent(id) == null) {
                    throw new NonexistentEntityException("The event with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Event event;
            try {
                event = em.getReference(Event.class, id);
                event.getIdEvent();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The event with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Invitation> invitationCollectionOrphanCheck = event.getInvitationCollection();
            for (Invitation invitationCollectionOrphanCheckInvitation : invitationCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Event (" + event + ") cannot be destroyed since the Invitation " + invitationCollectionOrphanCheckInvitation + " in its invitationCollection field has a non-nullable event field.");
            }
            Collection<Notification> notificationCollectionOrphanCheck = event.getNotificationCollection();
            for (Notification notificationCollectionOrphanCheckNotification : notificationCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Event (" + event + ") cannot be destroyed since the Notification " + notificationCollectionOrphanCheckNotification + " in its notificationCollection field has a non-nullable idEvent field.");
            }
            Collection<Warningevent> warningeventCollectionOrphanCheck = event.getWarningeventCollection();
            for (Warningevent warningeventCollectionOrphanCheckWarningevent : warningeventCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Event (" + event + ") cannot be destroyed since the Warningevent " + warningeventCollectionOrphanCheckWarningevent + " in its warningeventCollection field has a non-nullable event field.");
            }
            Collection<Commentwritten> commentwrittenCollectionOrphanCheck = event.getCommentwrittenCollection();
            for (Commentwritten commentwrittenCollectionOrphanCheckCommentwritten : commentwrittenCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Event (" + event + ") cannot be destroyed since the Commentwritten " + commentwrittenCollectionOrphanCheckCommentwritten + " in its commentwrittenCollection field has a non-nullable event field.");
            }
            Collection<Comment> commentCollectionOrphanCheck = event.getCommentCollection();
            for (Comment commentCollectionOrphanCheckComment : commentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Event (" + event + ") cannot be destroyed since the Comment " + commentCollectionOrphanCheckComment + " in its commentCollection field has a non-nullable idEvent field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            User idCreator = event.getIdCreator();
            if (idCreator != null) {
                idCreator.getEventCollection().remove(event);
                idCreator = em.merge(idCreator);
            }
            Collection<Schedule> scheduleCollection = event.getScheduleCollection();
            for (Schedule scheduleCollectionSchedule : scheduleCollection) {
                scheduleCollectionSchedule.getEventCollection().remove(event);
                scheduleCollectionSchedule = em.merge(scheduleCollectionSchedule);
            }
            em.remove(event);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Event> findEventEntities() {
        return findEventEntities(true, -1, -1);
    }

    public List<Event> findEventEntities(int maxResults, int firstResult) {
        return findEventEntities(false, maxResults, firstResult);
    }

    private List<Event> findEventEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Event.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Event findEvent(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Event.class, id);
        } finally {
            em.close();
        }
    }

    public int getEventCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Event> rt = cq.from(Event.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
